#!/usr/bin/env node
const mysql=require('mysql');
const fs=require('fs')
const connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : 'chzh5188',
    database : 'douban_movie'
});

connection.connect();

const sq=(query)=>{
    return new Promise((resolve,reject)=>{
        connection.query(query,(err,res)=>{
            if(err){
                reject(err);
            }
            resolve(res);
        })
    })
};

sq('select count(*) as `total` from `rating`;')
    .then(res=>{
        console.log(`共有${res[0].total}条数据`);
        return res[0].total;
    })
    .then(index=>{
        for (let i = 0; i <index; i++) {
            sq("select * from `rating` where `id`="+i)
                .then(res=>{
                    const item=res[0];
                    fs.writeFile('./input.txt',`${item.u_id}\t${item.m_id}:${item.rate}\r\n`,{flag:'a'},err=>{
                        if (err) console.log(err);
                    })
                    if(i%1000===0){
                        console.log(`${i}/${index}`)
                    }
                })
        }
    })

